package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.mapper.UserMapper;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.constants.Constants;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public List<User> getUserListByMap(Map<String, Object> param) {
        return userMapper.getUserListByMap(param);
    }

    @Override
    public Integer getUserCountByMap(Map<String, Object> param) {
        return userMapper.getUserCountByMap(param);
    }

    @Override
    public Integer qdtxAddUser(User user) {
        user.setUserCreatTime(new Date());
        return userMapper.insertUser(user);
    }

    @Override
    public Integer qdtxModifyUser(User user) {
        user.setUserModifiTime(new Date());
        return userMapper.updateUser(user);
    }

    @Override
    public Integer qdtxDeleteUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public Integer qdtxBatchDeleteUser(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return userMapper.batchDeleteUser(param);
    }

    @Override
    public Page<User> queryUserPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = userMapper.getUserCountByMap(param);
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<User> userList = userMapper.getUserListByMap(param);
        page.setList(userList);
        return page;
    }

    @Override
    public User getUserByPhone(String phone) throws EstSQLException {//登录
        //1.去数据库中查询是否存在此用户
        User user = userMapper.getUserByPhone(phone);

        //2.若没有存在则生成此用户放在数据库中
        if (user == null) {
            //数据库不存在此用户，需要插入此用户到user表中
            User u = new User();
            u.setUserPhone(phone);
            u.setUserRole(0);
            u.setUserCreatTime(new Date());
            int result = userMapper.insertUser(u);
            if (result <= 0) {
                throw new EstSQLException("数据库异常插入数据失败");
            }
            return u;
        }

        //3.若存在则返回对象信息
        return user;
    }

    @Override
    public User getUserByPhoneOpenid(String phone, String userWeChatOpenId) {
        return userMapper.getUserByPhoneOpenid(phone, userWeChatOpenId);
    }

    //根据用户名和性别模糊查询,当前页码和页容量分页（未拉黑）
    @Override
    public List<User> userSelectPage(@Param(value = "userName") String userName,
                                     @Param(value = "userSex") Integer userSex,
                                     @Param(value = "pageNo") Integer pageNo,
                                     @Param(value = "pageSize") Integer pageSize) {
        return userMapper.userSelectPage(userName, userSex, pageNo, pageSize);
    }

    @Override
    public Integer userSelectCount(@Param(value = "userName") String userName,
                                   @Param(value = "userSex") Integer userSex) {
        return userMapper.userSelectCount(userName, userSex);
    }

    //根据用户名和性别模糊查询,当前页码和页容量分页（拉黑）
    @Override
    public List<User> userSelectPage1(@Param(value = "userName") String userName,
                                     @Param(value = "userSex") Integer userSex,
                                     @Param(value = "pageNo") Integer pageNo,
                                     @Param(value = "pageSize") Integer pageSize) {
        return userMapper.userSelectPage1(userName, userSex, pageNo, pageSize);
    }

    @Override
    public Integer userSelectCount1(@Param(value = "userName") String userName,
                                   @Param(value = "userSex") Integer userSex) {
        return userMapper.userSelectCount1(userName, userSex);
    }

}